package org.example.iot;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Test0x35 {

    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.group(worker);
        bootstrap.option(NioChannelOption.TCP_NODELAY, true);
        bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                    //和服务器建立连接之后的回调
                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("连接建立");
                        int num=1;
                        while (true){
                            ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();

                            byte[] decodeHexDump = ByteBufUtil.decodeHexDump("23072b41433a31393a38453a36433a43413a3139444132303938383136202020202020204552313" +
                                    "23331323331323331323331310000018f6dd8d5bf40981a6d77318fc54096ff381d7dbf484098137f9724" +
                                    "74544096fe31a9fbe76dbfca1cac083126e93fd20ded288ce70445523132333132333132333132333131000" +
                                    "0018f6dd8d68840981a8a71de69ad4096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfc2339c0ebe" +
                                    "dfa43fcd9e83e425aee6455231323331323331323331323331310000018f6dd8d75040981a6c083126e94096ff1b" +
                                    "573eab364098137f972474544096fe31a9fbe76dbfc525460aa64c303fcf0a3d70a3d70a4552313233313233313" +
                                    "23331323331310000018f6dd8d81840981a9460aa64c34096ff10ff9724744098137f972474544096fe31a9fbe76" +
                                    "dbfc1930be0ded2893fce2b6ae7d566cf455231323331323331323331323331310000018f6dd8d8e140981a8a71de" +
                                    "69ad4096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfc42c3c9eecbfb13fcccccccccccccd45523132333" +
                                    "1323331323331323331310000018f6dd8d9ae40981a7573eab3684096ff0ff97247454098137f972474544096fe31a9f" +
                                    "be76dbfc5495182a9930c3fce90ff97247454455231323331323331323331323331310000018f6dd8da7040981a8a71d" +
                                    "e69ad4096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfa50b0f27bb2fec3fca0f9096bb98c845523132333" +
                                    "1323331323331323331310000018f6dd8db3940981a7089a027524096ff243fe5c91d4098137f972474544096fe31a9fbe" +
                                    "76dbfc6d5cfaacd9e843fd0ea4a8c154c98455231323331323331323331323331310000018f6dd8dc0140981a6f1a9fbe77" +
                                    "4096ff075f6fd2204098137f972474544096fe31a9fbe76dbfc9930be0ded2893fcb15b573eab36845523132333132333132" +
                                    "3331323331310000018f6dd8dccc40981a80ebedfa444096ff30f27bb2ff4098137f972474544096fe31a9fbe76dbfc16bb98" +
                                    "c7e28243fd2d42c3c9eecc0455231323331323331323331323331310000018f6dd8dd9040981a85f06f69444096ff1cac08312" +
                                    "74098137f972474544096fe31a9fbe76dbfc5013a92a305533fcdf06f6944673845523132333132333132333132333131000001" +
                                    "8f6dd8de5840981a8c63f141204096ff1e9e1b089a4098137f972474544096fe31a9fbe76dbfc60aa64c2f837b3fce90ff972474" +
                                    "54455231323331323331323331323331310000018f6dd8df2140981a80ebedfa444096ff30f27bb2ff4098137f972474544096fe3" +
                                    "1a9fbe76dbfc43fe5c91d14e43fd40346dc5d6388455231323331323331323331323331310000018f6dd8dfeb40981a95e9e1b08a4" +
                                    "096fefa92a305534098137f972474544096fe31a9fbe76dbfc315b573eab3683fc972474538ef354552313233313233313233313233" +
                                    "31310000018f6dd8e0b040981a95e9e1b08a4096fefa92a305534098137f972474544096fe31a9fbe76dbfc113404ea4a8c13fca3d70" +
                                    "a3d70a3d455231323331323331323331323331310000018f6dd8e17a40981a6f1a9fbe774096ff075f6fd2204098137f972474544096" +
                                    "fe31a9fbe76dbfc74538ef34d6a13fcbeab367a0f909455231323331323331323331323331310000018f6dd8e24140981a8a71de69ad4" +
                                    "096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfb7e28240b780343fcb39c0ebedfa44455231323331323331323331323331" +
                                    "310000018f6dd8e30b40981a8a71de69ad4096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfc6a7ef9db22d0e3fcc1205bc01" +
                                    "a36e455231323331323331323331323331310000018f6dd8e3d140981aa64c2f837b4096ff074538ef354098137f972474544096fe31a9" +
                                    "fbe76dbfb810624dd2f1aa3fce2eb1c432ca58455231323331323331323331323331310000018f6dd8e4a040981a8a71de69ad4096ff0cc" +
                                    "ccccccd4098137f972474544096fe31a9fbe76dbfc6a7ef9db22d0e3fcc1205bc01a36e455231323331323331323331323331310000018f" +
                                    "6dd8e56140981a5d14e3bcd34096ff2b6ae7d5674098137f972474544096fe31a9fbe76dbfd0d35a858793de3fd13dd97f62b6ae4552313" +
                                    "23331323331323331323331310000018f6dd8e62a40981a7573eab3684096ff0ff97247454098137f972474544096fe31a9fbe76dbfc4af" +
                                    "4f0d844d013fcdcfaacd9e83e4455231323331323331323331323331310000018f6dd8e6f040981a6f1a9fbe774096ff075f6fd220409813" +
                                    "7f972474544096fe31a9fbe76dbfc74538ef34d6a13fcbeab367a0f909455231323331323331323331323331310000018f6dd8e7ba40981a6" +
                                    "f1a9fbe774096ff075f6fd2204098137f972474544096fe31a9fbe76dbfcbfe5c91d14e3c3fca617c1bda511a455231323331323331323331" +
                                    "323331310000018f6dd8e88040981a8a71de69ad4096ff0ccccccccd4098137f972474544096fe31a9fbe76dbfc42c3c9eecbfb13fccbc6a7" +
                                    "ef9db230d0a");
                            buffer.writeBytes(decodeHexDump);
                            ctx.writeAndFlush(buffer);
                            Thread.sleep(500);
                            num++;
                            System.out.println(num);
                        }
                    }
                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("连接断开了");
                        ctx.fireChannelInactive();
                    }
                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        System.out.println("发送异常了");
                        ctx.fireExceptionCaught(cause);
                    }
                });
            }
        });
        ChannelFuture channelFuture = bootstrap.connect("iottcp.caups.cn", 9876).sync();
        Channel channel = channelFuture.channel();

    }
}
